package com.smedi.ismedi.file.core.port.adapter.fastdfs;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.smedi.ismedi.file.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.Charset;


/**
 * @Author Jimmy
 * @Date 2021/6/3 14:22
 */
@Component
@AllArgsConstructor
@Slf4j
public class FastDFSClient {
    private final FastFileStorageClient storageClient;

    private final FdfsWebServer fdfsWebServer;

    /**
     * 上传文件
     *
     * @param file 文件对象
     * @return 文件访问地址
     * @throws IOException
     */
    public StorePath uploadFile(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        long size = file.getSize();
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        log.debug("文件大小:{},文件后缀名：{}",size,extension);
        return storageClient.uploadFile(inputStream, size, extension, null);
    }


    /**
     * 上传文件
     *
     * @param file 文件对象
     * @return 文件访问地址
     * @throws IOException
     */
    public StorePath uploadFile(File file) throws FileNotFoundException {
        FileInputStream inputStream = new FileInputStream(file);
        StorePath storePath = storageClient.uploadFile(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
        return storePath;
    }

    /**
     * 将一段字符串生成一个文件上传
     *
     * @param content       文件内容
     * @param fileExtension
     * @return
     */
    public String uploadFile(String content, String fileExtension) {
        byte[] buff = content.getBytes(Charset.forName("UTF-8"));
        ByteArrayInputStream stream = new ByteArrayInputStream(buff);
        StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtension, null);
        return getResAccessUrl(storePath);
    }

    // 封装图片完整URL地址
    private String getResAccessUrl(StorePath storePath) {
        String fileUrl = fdfsWebServer.getWebServerUrl() + storePath.getFullPath();
        return fileUrl;
    }

    /**
     * 删除文件
     *
     * @param fileUrl 文件访问地址
     * @return
     */
    public void deleteFile(String fileUrl) {
        if (StringUtils.isEmpty(fileUrl)) {
            return;
        }
        try {
            StorePath storePath = StorePath.parseFromUrl(fileUrl);
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (FdfsUnsupportStorePathException e) {
            throw LogicException.raise(CustomError.FILE_DELETE_ERROR);
        }
    }


    /**
     * 删除文件
     *
     * @param group
     * @param path
     */
    public void deleteFile(String group, String path) {
        try {
            storageClient.deleteFile(group, path);
        } catch (FdfsUnsupportStorePathException e) {
            throw LogicException.raise(CustomError.FILE_DELETE_ERROR);
        }
    }

    /**
     * 下载文件
     *
     * @param group
     * @param path
     * @return
     * @throws IOException
     */
    public byte[] downloadFile(String group, String path) throws IOException {
        byte[] bytes = null;
        DownloadByteArray byteArray = new DownloadByteArray();
        bytes = storageClient.downloadFile(group, path, byteArray);
        return bytes;
    }

}
